﻿using System.Configuration;

namespace smp.RabbitMq.Configuration
{
    public class RabbitMqSection : ConfigurationSection
    {
        [ConfigurationProperty("publishers")]
        public PubllishConnections Publishers
        {
            get { return (PubllishConnections) this["publishers"]; }
        }

        [ConfigurationProperty("requesters")]
        public RequestConnections Requesters
        {
            get { return (RequestConnections)this["requesters"]; }
        }
        
        [ConfigurationProperty("subscriber")]
        public SubscribeConnection Subscriber
        {
            get { return (SubscribeConnection) this["subscriber"]; }
        }
        
        [ConfigurationProperty("defaulttimeout", IsRequired = false)]
        public int? DefaultTimeOut
        {
            get { return (int?) this["defaulttimeout"]; }
        }

        internal IRabbitMqConfiguration GetPublisher(string key)
        {
            var serversfromConfig = ((RabbitMqSection)ConfigurationManager.GetSection("RabbitMqSection"));
            if (serversfromConfig == null)
            {
                throw new ConfigurationErrorsException("Invalid or missing RabbitMq configuration element : RabbitMqSection");
            }
            if (serversfromConfig.Publishers == null || serversfromConfig.Publishers.Count == 0)
            {
                throw new ConfigurationErrorsException("No Publishers listed under RabbitMqSection Connections");
            }
            foreach (PublishConnection connection in serversfromConfig.Publishers)
            {
                if (connection.Key == key)
                    return connection.GetConfiguration();
            }
            throw new ConfigurationErrorsException("Unable to find RabbitMq configuration for rabbit publish key " + key);
        }

        internal IRabbitMqConfiguration GetRequester(string key)
        {
            var serversfromConfig = ((RabbitMqSection)ConfigurationManager.GetSection("RabbitMqSection"));
            if (serversfromConfig == null)
            {
                throw new ConfigurationErrorsException("Invalid or missing RabbitMq configuration element : RabbitMqSection");
            }
            if (serversfromConfig.Requesters == null || serversfromConfig.Requesters.Count == 0)
            {
                throw new ConfigurationErrorsException("No Requesters listed under RabbitMqSection Connections");
            }
            foreach (RequestConnection connection in serversfromConfig.Requesters)
            {
                if (connection.Key == key)
                    return connection.GetConfiguration();
            }
            throw new ConfigurationErrorsException("Unable to find RabbitMq configuration for rabbit request key " + key);
        }

        internal IRabbitMqConfiguration GetSubscriber()
        {
            var serversfromConfig = ((RabbitMqSection)ConfigurationManager.GetSection("RabbitMqSection"));
            if (serversfromConfig == null)
            {
                throw new ConfigurationErrorsException("Invalid or missing RabbitMq configuration element : RabbitMqSection");
            }
            if (serversfromConfig.Subscriber == null)
            {
                throw new ConfigurationErrorsException("No Subscriber listed under RabbitMq Section");
            }
            return serversfromConfig.Subscriber.GetConfiguration();
        }

        internal int GetDefaultTimeOut()
        {
            var serversfromConfig = ((RabbitMqSection)ConfigurationManager.GetSection("RabbitMqSection"));
            if (serversfromConfig == null)
            {
                throw new ConfigurationErrorsException("Invalid or missing RabbitMq configuration element : RabbitMqSection");
            }
            if (serversfromConfig.Subscriber == null)
            {
                throw new ConfigurationErrorsException("No Subscriber listed under RabbitMq Section");
            }
            return serversfromConfig.DefaultTimeOut.HasValue ? serversfromConfig.DefaultTimeOut.Value : 60000;
        }
    }
}